/*
 * 文 件 名:  DispatchAction.java
 * 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2013,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  yanglong
 * 修改时间:  2013-11-10
 * 跟踪单号:  <需求跟踪单号>
 * 修改单号:  <需求修改单号>
 * 修改内容:  <修改内容>
 */
package com.hfut.jpoa.action;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.action.AbsAction;
import com.hfut.jpoa.common.dto.Pagination;
import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.ConstantUtil;
import com.hfut.jpoa.common.util.DateUtils;
import com.hfut.jpoa.dto.CustomerDTO;
import com.hfut.jpoa.model.Customer;
import com.hfut.jpoa.model.Syspara;
import com.hfut.jpoa.service.CustomerService;
import com.hfut.jpoa.service.SysparaService;
import com.hfut.jpoa.service.exception.CustomerException;
import com.hfut.jpoa.service.exception.SysparaException;

/**
 * 客户服务模块
 * 
 * @author yanglong
 * @version [1.0, 2013-11-10]
 * @since [产品/1.0]
 */
public class CustomerAction extends AbsAction<CustomerDTO>
{

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(CustomerAction.class);

	private static final long serialVersionUID = -7819502638545092791L;

	private Customer customer;

	/**
	 * 标志位：0-增加 1-修改 2-详情
	 */
	private String actionType;

	/**
	 * 页面类型：0：调度中心 1：售后部
	 */
	private String pageType;

	/**
	 * 包含分页接受前台变量
	 */
	private CustomerDTO customerDTO;

	/**
	 * 分页结果集
	 */
	private ResultDto<Customer> resultDto;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 创建者,以后从session中取
	 */
	private String creator = "yl";

	/**
	 * 客户服务
	 */
	private CustomerService customerService;

	/**
	 * 字典表服务
	 */
	private SysparaService sysparaService;

	/**
	 * 路线列表
	 */
	private List<Syspara> lstPath;

	/**
	 * 客户类别列表
	 */
	private List<Syspara> lstCustomerType;

	/**
	 * 客户列表
	 */
	private List<Customer> lstCustomers;

	/**
	 * 列表页面初始化
	 * 
	 * @return
	 * @throws CustomerException
	 */
	public String listInit() throws CustomerException
	{

		if (null == customerDTO)
		{
			customerDTO = new CustomerDTO();
		}
		Pagination pagination = new Pagination(0);
		customerDTO.setPagination(pagination);
		customerDTO.setCustomer(customer == null ? new Customer() : customer);
		listDate(customerDTO);
		return "customer_list_init";
	}

	/**
	 * 加载列表数据
	 * 
	 * @return
	 */
	public String list() throws CustomerException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (customerDTO == null)
		{
			customerDTO = new CustomerDTO();
		}

		customerDTO.setPagination(pagination);
		listDate(customerDTO);
		return "customer_list";
	}

	private void listDate(CustomerDTO dto) throws CustomerException
	{
		try
		{
			resultDto = customerService.queryInfoForPage(dto);
		} catch (CustomerException e)
		{
			LOGGER.error("加载客户信息异常：", e);
			throw new CustomerException("加载客户信息异常：", e);
		}
		totalPage = resultDto.getPagination().getTotalPage();
	}

	/**
	 * 预加载客户弹出层
	 * 
	 * @return
	 * @throws CustomerException
	 */
	public String initCustomersDialog() throws CustomerException
	{

		return "customer_dialog_init";
	}

	/**
	 * 加载客户列表选择弹出层
	 */
	public String listCustomersDialog() throws CustomerException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (customerDTO == null)
		{
			customerDTO = new CustomerDTO();
		}
		if (customerDTO.getCustomer() == null)
		{
			Customer cus = new Customer();
			customerDTO.setCustomer(cus);
		}
		customerDTO.getCustomer().setState(ConstantUtil.STATE_Y);
		customerDTO.setPagination(pagination);
		listDate(customerDTO);
		return "customer_dialog_list";
	}

	/**
	 * 加载客户信息列表
	 * 
	 * @return
	 * @throws CustomerException
	 */
	public String initCustomer() throws CustomerException
	{

		Customer customer = new Customer();
		customer.setState(ConstantUtil.STATE_Y);
		queryCustomers(customer);
		return "init_customer_list";
	}

	/**
	 * 加载客户列表
	 * 
	 * @param user
	 * @return
	 * @throws CustomerException
	 */
	public List<Customer> queryCustomers(Customer customer)
	        throws CustomerException
	{
		if (null == customer)
		{
			customer = new Customer();
		}
		customer.setState(ConstantUtil.STATE_Y);
		List<Customer> lstCustomers;
		try
		{
			lstCustomers = customerService.queryCustomers(customer);
		} catch (CustomerException e)
		{
			LOGGER.error("加载客户信息异常：", e);
			throw new CustomerException("加载客户信息异常", e);
		}
		return lstCustomers;
	}

	/**
	 * 增加页面初始化
	 */
	public String addInit() throws CustomerException
	{
		try
		{
			// 从字典表中加载路线
			Syspara syspara = new Syspara();
			syspara.setType(ConstantUtil.PATH_ID);
			lstPath = sysparaService.querySysparaList(syspara);
			// 从字典表中加载客户类别
			syspara.setType(ConstantUtil.CUSTOMER_TYPE);
			lstCustomerType = sysparaService.querySysparaList(syspara);

		} catch (SysparaException e)
		{
			LOGGER.error("增加页面初始化异常：", e);
			throw new CustomerException("增加页面初始化异常：", e);
		}
		actionType = ConstantUtil.ACTIONTYPE_ADD;
		return "customer_add_init";
	}

	/**
	 * 根据客户级别查询客户列表
	 * 
	 * @throws IOException
	 */
	public void queryCustomers() throws CustomerException, IOException
	{
		if (null == customer)
		{
			LOGGER.error("根据客户级别查询客户列表异常");
			failure("根据客户级别查询客户列表异常");
			throw new CustomerException("根据客户级别查询客户列表异常");
		}
		lstCustomers = customerService.queryCustomers(customer);
		success(lstCustomers);
	}

	/**
	 * 新增
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String addCustomer() throws CustomerException, ParseException
	{
		customer.setCreateDate(DateUtils.getNowDate());
		customer.setLastUpdateDate(DateUtils.getNowDate());
		customerService.saveCustomer(customer);
		return "customer_list_init";
	}

	/**
	 * 修改页面初始化
	 * 
	 * @throws ParseException
	 */
	public String customerUpdateInit() throws CustomerException
	{
		try
		{
			// 从字典表中加载路线
			Syspara syspara = new Syspara();
			syspara.setType(ConstantUtil.PATH_ID);
			lstPath = sysparaService.querySysparaList(syspara);
			// 从字典表中加载客户类别
			syspara.setType(ConstantUtil.CUSTOMER_TYPE);
			lstCustomerType = sysparaService.querySysparaList(syspara);

		} catch (SysparaException e)
		{
			LOGGER.error("修改页面初始化异常：", e);
			throw new CustomerException("修改页面初始化异常：", e);
		}
		customer = customerService.findCustomer(customer);
		actionType = ConstantUtil.ACTIONTYPE_UPDATE;
		return "customer_add_init";
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws CustomerException
	 */
	public String updateCustomer() throws CustomerException, ParseException
	{
		customer.setLastUpdateDate(DateUtils.getNowDate());
		customerService.updateCustomer(customer);
		// dispatchService.updateDispatch(dispatch);
		return "customer_list_init";
	}

	/**
	 * 修改字典表状态
	 */
	public void updateState()
	{
		/*
		 * try { // dispatchService.updateDispatch(dispatch); } catch
		 * (CustomerException e1) { try { failure("操作异常：" + e1.getMessage()); }
		 * catch (IOException e) { LOGGER.error("修改字典配置异常", e); } } try {
		 * success("成功操作！"); } catch (IOException e) { LOGGER.error("修改字典配置异常",
		 * e); }
		 */
	}

	/**
	 * 验证编码是否存在
	 * 
	 * @throws IOException
	 */
	public void codeValidator() throws IOException
	{
		/*
		 * boolean flag = false; try { flag =
		 * dispatchService.codeValidator(dispatch); } catch (CustomerException
		 * e) { LOGGER.error("验证编码是否存在异常", e); failure("验证编码是否存在异常" +
		 * e.getMessage()); } if (flag) { failure("该编码已存在，请重新输入"); } else {
		 * success("输入正确，该编码可以使用"); }
		 */
	}

	public String getActionType()
	{
		return actionType;
	}

	public void setActionType(String actionType)
	{
		this.actionType = actionType;
	}

	public int getTotalPage()
	{
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		this.totalPage = totalPage;
	}

	public String getPageType()
	{
		return pageType;
	}

	public void setPageType(String pageType)
	{
		this.pageType = pageType;
	}

	public CustomerService getCustomerService()
	{
		return customerService;
	}

	public void setCustomerService(CustomerService customerService)
	{
		this.customerService = customerService;
	}

	public CustomerDTO getCustomerDTO()
	{
		return customerDTO;
	}

	public void setCustomerDTO(CustomerDTO customerDTO)
	{
		this.customerDTO = customerDTO;
	}

	public Customer getCustomer()
	{
		return customer;
	}

	public void setCustomer(Customer customer)
	{
		this.customer = customer;
	}

	public SysparaService getSysparaService()
	{
		return sysparaService;
	}

	public void setSysparaService(SysparaService sysparaService)
	{
		this.sysparaService = sysparaService;
	}

	public List<Syspara> getLstPath()
	{
		return lstPath;
	}

	public void setLstPath(List<Syspara> lstPath)
	{
		this.lstPath = lstPath;
	}

	public ResultDto<Customer> getResultDto()
	{
		return resultDto;
	}

	public void setResultDto(ResultDto<Customer> resultDto)
	{
		this.resultDto = resultDto;
	}

	public List<Customer> getLstCustomers()
	{
		return lstCustomers;
	}

	public void setLstCustomers(List<Customer> lstCustomers)
	{
		this.lstCustomers = lstCustomers;
	}

	public List<Syspara> getLstCustomerType()
	{
		return lstCustomerType;
	}

	public void setLstCustomerType(List<Syspara> lstCustomerType)
	{
		this.lstCustomerType = lstCustomerType;
	}

}
